// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue';
import App from './App';
import router from './router';
import '@/assets/css/base.css';
// import bridge from '@/assets/js/adapter';
import vConsoleDebug from '@/assets/js/vConsoleDebug';
import Axios from 'axios';
import vueEsign from 'vue-esign';
import toast from '@/components/Toast';
import '@/assets/js/vant-reg';
import {login} from '@/assets/js/api';
vConsoleDebug.initVConsole();
Vue.use(vueEsign);
Vue.prototype.$toast = toast;
Vue.config.productionTip = false;
Axios.defaults.baseURL = 'https://www.qianduoduocaishang.com';
Axios.defaults.headers = {
};
const NEED_LOGIN = ['personalInfo', 'agreementList', 'agreementDetail', 'qaList', 'tryOut'];
router.beforeEach((to, from, next) => {
    if (NEED_LOGIN.includes(to.name) && !localStorage.userId) {
        login().then(url => {
            window.location.href = url;
        });
    } else {
        next();
    }
});

// 添加一个请求拦截器
Axios.interceptors.request.use(function(config) {
    // console.log(config);
    // config.headers.Token = localStorage.getItem('token');
    return config;
}, function(err) {
    return Promise.reject(err);
});

// 添加一个响应拦截器
Axios.interceptors.response.use(function(res) {
    // vConsoleDebug.logOriginData(res);
    let data = res.data;
    if (data.state !== 0) {
        return data.data;
    }
    toast(data.msg);
    return Promise.reject(data);
}, function(err) {
    toast('网络故障，请稍后再试');
    return Promise.reject(err);
});
/* eslint-disable no-new */
new Vue({
    el: '#app',
    router,
    components: { App },
    template: '<App/>'
});
